package it.vetlab;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

public final class VetLabElFunctions {

	private VetLabElFunctions() {
	}

	public static PersistenceManager getPersistenceManager() {
		return PMF.get().getPersistenceManager();
	}

	public static void closePersistenceManager(PersistenceManager pm) {
		pm.close();
	}

	@SuppressWarnings("unchecked")
	public static List<User> listUsers(PageContext ctx) {
		HttpServletRequest req = (HttpServletRequest) ctx.getRequest();
		PersistenceManager pm = (PersistenceManager) req
				.getAttribute("persistenceManager");
		if (pm == null) {
			pm = PMF.get().getPersistenceManager();
			req.setAttribute("persistenceManager", pm);
		}
		Query query = pm.newQuery(User.class);
//		query.setOrdering("lastName ASC, firstName ASC");
		return (List<User>) query.execute();
	}
}
